<?php
class Image_resizer
{
	public $errors;
	private $_image;
	
	public function __construct()
	{
		$this->errors = array();
		$this->_image = NULL;
		$this->ext = null;
		$this->cache_folder = "./assets/asset-store/blog/cache/";
		$this->url = null;
	}
	
	public function getImage($url)
	{
		$this->url = $url;
		$this->ext = strrchr($this->url, '.');
		$this->ext = strtolower($this->ext);
		
		switch($this->ext) {
			case '.jpeg':
			case '.jpg':
				$this->_image = imagecreatefromjpeg($this->url);
				break;
			case '.gif':
				$this->_image = imagecreatefromgif($this->url);
				break;
			case '.png':
				$this->_image = imagecreatefrompng($this->url);
			default:
				$this->errors[] = "The linked file must be a .jpeg, .jpg, .gif, or .png";
				break;
		}
	}
	
	public function resizeImage($new_width, $new_height, $should_crop = 0)
	{
		if($this->_image)
		{
			//required image width and height
			$destination_width = $new_width;
			$destination_height = $new_height;
			$destination_ratio = ($destination_width / $destination_height);
		
			//main image width and height
			$source_width = imagesx($this->_image);
			$source_height = imagesy($this->_image);
			$source_ratio = ($source_width / $source_height);

			if($should_crop == 1)
			{
				// crop to fit 
				if ($source_ratio > $destination_ratio) { 
					// source has a wider ratio 
					$temp_width = (int)($source_height * $destination_ratio); 
					$temp_height = $source_height; 
					$source_x = (int)(($source_width - $temp_width) / 2); 
					$source_y = 0; 
				} else { 
					// source has a taller ratio 
					$temp_width = $source_width; 
					$temp_height = (int)($source_width / $destination_ratio); 
					$source_x = 0; 
					$source_y = (int)(($source_height - $temp_height) / 2); 
				} 
				$destination_x = 0; 
				$destination_y = 0; 
				$source_width = $temp_width; 
				$source_height = $temp_height; 
				$new_destination_width = $destination_width; 
				$new_destination_height = $destination_height; 
			}
			else
			{
				// dont crop but just resize to ratio
				if($source_width > $source_height) 
				{
					$ratio = $source_height / $destination_height;
				}
				else
				{
					$ratio = $source_width / $destination_width;
				}
				$temp_width = ceil($source_width / $ratio);
				$temp_height = ceil($source_height / $ratio);
				
				$source_x = 0; 
				$source_y = 0; 
				$destination_x = 0; 
				$destination_y = 0; 
				$new_destination_width = $temp_width; 
				$new_destination_height = $temp_height; 

				//reset the actually resize require and base it on the ratio size
				$destination_width = $temp_width; 
				$destination_height = $temp_height; 
			}

			$destination_image = imagecreatetruecolor($destination_width, $destination_height);
			imagecopyresampled($destination_image, $this->_image, $destination_x, $destination_y, $source_x, $source_y, $new_destination_width, $new_destination_height, $source_width, $source_height); 
			$this->_image = $destination_image;

			switch($this->ext) {
				case '.jpeg':
				case '.jpg':
					imagejpeg($destination_image, $this->cache_folder.basename($this->url), 90);
					break;
				case '.gif':
					imagegif($destination_image, $this->cache_folder.basename($this->url), 90);
					break;
				case '.png':
					imagepng($destination_image, $this->cache_folder.basename($this->url), 9);
				default:
					$this->errors[] = "The linked file must be a .jpeg, .jpg, .gif, or .png";
					break;
			}
		}
		//exit;
	}
	
	public function displayImage()
	{
		if($this->_image)
		{
			switch($this->ext) {
				case '.jpeg':
				case '.jpg':
					header('Content-type: image/jpeg');
					imagejpeg($this->_image, NULL, 100);
					break;
				case '.gif':
					header('Content-type: image/gif');
					imagegif($this->_image, NULL, 100);
					break;
				case '.png':
					header('Content-type: image/png');
					imagepng($this->_image, NULL, 9);
				default:
					echo "error";
					break;
			}
			
			imagedestroy($this->_image);	
		}
	}
	
}
?>